const net = require('../../utils/net.js')

// index.js
// 获取应用实例
const app = getApp()

Page({
  data: {
    msg: 'hiha',
    isLogin: false,
    a: 10,
    b: 20,
    c: 100,
    ad_datas: [],
    current_ad_title: '',
    shows: [],
    page: 1,
    pages: 1,
    city: ''
  },
  changeMsg(){
    console.info('--已点我了---')
    console.info(this.data.msg)
    this.setData({
      msg: '我是disen',
      isLogin: !this.data.isLogin
    })
  },
  itemTapHandler(e){
    console.info(e)
    this.setData({
      msg: e.detail.title
    })
  },
  onPullDownRefresh(){
    this.setData({
      page: 1
    })
    this.loadShows('',0)
  },

  loadShows(city, opt=1){
    wx.setNavigationBarTitle({title: city})
    net.request({url: '/show/?city='+city+'&page=' + this.data.page}).then(resp=>{
      console.info(resp.data)
      const { data, page, pages } = resp.data
        this.setData({
        shows: opt == 1?this.data.shows.concat(data): data,
        page: page,
        pages: pages
      })
    })
  },
  onReady(){
    // 获取当前定位的信息
    wx.getLocation({
      type:'gcj02 ',
      isHighAccuracy: true,
      success: res => {
        const {latitude, longitude} = res
        
        wx.setStorageSync('location', {lat: latitude, lng:longitude})

        //根据当前的定位，获取城市
        getApp().qqmapsdk.reverseGeocoder({
          location: {latitude, longitude},
          success: res => {
            const {city} = res.result.ad_info
            wx.setStorageSync('city', city)

            // this.loadShows(city)

          }
        })
      }
    })

    net.request({url: '/show/all_music/'}).then(resp=>{
      console.info(resp.data)
      // 显示音乐人的推荐
      this.setData({
        ad_datas: resp.data.slice(0, 5),
        current_ad_title: resp.data[0].msc_name
      })
      console.info(this.data.ad_datas)
    })
   
    this.loadShows('')

   
  },
  adChange(e){
    const {current, source } = e.detail
    this.setData({
      current_ad_title: this.data.ad_datas[current].msc_name
    })
  },
  showDetail(e){
    console.info(e)
    const {show_id} = e.target.dataset
    console.info('--显示的ID---', show_id)
    wx.navigateTo({
      url: "/pages/detail/detail?show_id="+show_id
    })
  },
  onReachBottom(){
    // 加载更多数据  [注意]：判断当前页号是否超出了总页数
    if(this.data.page < this.data.pages){
      this.setData({
        page:this.data.page + 1
      })
      this.loadShows('')
    }else{
      wx.showToast({
        title: '别拉了，到底了',
      })
    }
  }
})
